<template>
  <div>
      <el-table border :data="videoList">
        <el-table-column prop="Vname"  label="视频名称"></el-table-column>
        <el-table-column prop="Vstate" label="绑定课程"></el-table-column>
        <el-table-column prop="Vtime" label="视频时长"></el-table-column>
         <el-table-column label="编辑日期">
           <template #default="scope">
             <div>
                {{new Date(scope.row.createAt).toLocaleString()}}
             </div>
           </template>
         </el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <div>
                <el-button v-if="!dirid" @click="senddata(scope.row)" type="success">编辑</el-button>
                 <el-button v-if="!dirid" @click="delVideos(scope.row.ID)"  type="danger">删除</el-button>
                 <el-button v-if="dirid" @click="addVideos(scope.row._id)" type="success">添加</el-button>
              </div>
            </template>
          </el-table-column>
      </el-table>
      <el-pagination @current-change="pageChange"  background layout="prev,pager,next"   :page-size="3"  :total="count">
      </el-pagination>
  </div>
</template>
<script lang="ts">
export default {
  props:["videoList","count"],
  setup(){

  }
}
</script>
<script lang="ts" setup>
  import {delVideo} from '../../api/video'
  import $bus from '../../utils/bus'
  import { ref, provide,inject,watch } from 'vue';
  import {addDirVideo} from '../../api/course'
  import {useRoute} from 'vue-router'
import { ElMessage } from 'element-plus';
    let emit=defineEmits(["pageChange"])
    let route=useRoute();
    let page=ref(1);
    let pageChange=(val:number)=>{
        emit("pageChange",val)
        page.value=val;
    }
    let dirid=ref("");
    // 获取到目录id
    $bus.on("senddir",(row:any)=>{
      console.log(row);
      dirid.value=row._id;
    })
    let delVideos=async (ID:any)=>{
        // 删除接口
        var res:any=await delVideo(ID)
        if(res.success){
            ElMessage.success("删除成功")
             emit("pageChange",page.value)
        }
    }
    // 点击编辑视频 当前行数据传给兄弟组件
    let senddata=(row:any)=>{
        $bus.emit("sendrow",row)
    }
    // 给对应的目录下添加视频
    let addVideos= async (_id:any)=>{
      var res:any=await addDirVideo({
           cid: route.query.c_id, //课程_id
          _id: dirid.value, //目录_id
          Vid: _id //视频_id
      })
       console.log(res)
       $bus.emit("sendvideolist",res.data.list)
       ElMessage.success("添加视频成功")
    }
</script>

<style lang="less">
  
</style>